;(function($){
	$.fn.dish_cpkz = function(opts){
		var el = this;
		var opt = {
			ds : [],
			cpkzMax : 0,
			dataBack : function(){},
			closeBack : function(){},
		}
		var param = $.extend(opt,opts);
		var app = new App(el,param);
	}
	
	var App = function(el,param){
		this.el = el;
		this.param = param;
		this.getCpkzMaxNum();
		this.initHmtl();
		this.initGrid();
		this.bindEvent();
	}
	
	App.prototype = {
		bindEvent : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			//返回
			el.find("#btnCancel").unbind("click").click(function(){
				
				el.empty();
				el.modal("hide");
				param.closeBack();
			})
			
			//确定
			el.find("#btnSure").unbind("click").click(function(){
				t.saveCpkz();
			});
			
			//全选
			el.find("#btnSelAll").unbind("click").click(function(){
				
				t.saveAllCpkz();
			});
			
			//取消拼菜
			el.find("#btnCancelCpkz").unbind("click").click(function(){
				t.saveCancelCpkz();
			});
		},
		initHmtl : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			el.addClass("modal").attr("tabindex","-2").attr("data-backdrop","static")
    		var html =	'<div class="modal-dialog remark_order remark_dish" style="width:800px;">'+
				    		'<div id="pageOverlay" class="hide"></div>'+
							'<div id="loading" class="loading hide">'+
								'<i class="ace-icon fa fa-spinner fa-spin orange bigger-250"></i>'+
							'</div>'+
							'<div class="modal-content" style="width:800px;">'+     
					  			'<div class="modal-header no-padding">'+
									'<div class="table-header widget-header-large " style="text-align:center; font-size:20px">'+  
										'<span>'+$.i18n.propJsp("拼菜")+'</span>'+       
									'</div>'+
								'</div>'+
								'<div class="modal-body">'+ 
									'<div class="widget-box">'+
										'<table id="modalGrid"></table>'+
//										'<div id="modalPage"></div>'+
									'</div>'+
								'</div>'+	
								'<div class="modal-footer">'+
									'<div class="clearfix">'+
										'<div class="pull-left">'+
											'<button id="btnSelAll" class="btn btn-info btn-lg btn-round">'+  
												'<i class="ace-icon fa fa-check"></i>'+$.i18n.propJsp("全拼")+
											'</button>'+
										'</div>'+
										'<div class="pull-right">'+
											'<button id="btnSure" class="btn btn-info btn-lg btn-round">'+  
												'<i class="ace-icon fa fa-check"></i>'+$.i18n.propJsp("确定")+
											'</button>'+
											'<button id="btnCancelCpkz" class="btn btn-grey btn-lg btn-round">'+  
												'<i class="ace-icon fa fa-trash"></i>'+$.i18n.propJsp("取消拼菜")+
											'</button>'+
											'<button id="btnCancel" class="btn btn-default btn-lg btn-round" data-dismiss="modal">'+  
												'<i class="ace-icon fa fa-undo"></i>'+$.i18n.propJsp("返回")+'</button>'+ 
											'</button>'+
										'</div>'+
									'</div>'+
								'</div>'+		
			  				'</div>'+
			  			'</div>';		
			el.html(html);
		},
		initGrid : function(){
			
			var el = this.el;
			var param = this.param;
			var t = this;
			
			var grid_selector = el.find("#modalGrid");
			
			grid_selector.jqGrid({
				datatype : "local",
				width :766,
				height : 300,
				data : [],
				colNames : ["名称","金额","数量","单位","拼"],
				colModel : [{
					name : "mc",//"名称",
					index : "mc",
					width: 200,
				},{
					name : "dj",//"金额",
					index : "dj",
					width: 100,
					align : "right",
				},{
					name : "sl",//"数量",
					index : "sl",
					width: 100,
					align : "right",
				},{
					name : "dw",//"单位",
					index : "dw",
					width: 100,
					align : "right",
				},{
					name : "cpkz",//"拼"
					index : "cpkz",
					width: 100,
					align : "right",
				}],    	
				scroll : true,
				sortname : "no",
				sortorder : "desc",
				viewrecords : true,
				emptyrecords: $.i18n.propJsp("没有搜索到任何数据"),
				recordtext: $.i18n.propJsp("共计")+":{2}"+$.i18n.propJsp("条"),
				altRows : true, 
				scrollrows : true,
				autoScroll : true,
				shrinkToFit : true,
				multiselect : true,
		        ondblClickRow : function(rowid){  
		        },
		        onSelectRow:function(rowid){
		        },
				loadComplete : function(data){
					var table = this;
					setTimeout(function(){
//						updatePagerIcons(table);
//						enableTooltips(table);
					}, 0);
					el.find("#cb_modalGrid").hide();
//					var ids = grid_selector.jqGrid("getGridParam","selarrrow");
//					grid_selector.jqGrid("setSelection",ids[0]);
				}, 
			});
			t.initGridDs();
		},
		initGridDs :function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			var grid_selector = el.find("#modalGrid");
			grid_selector.jqGrid("clearGridData");
			$(param.ds).each(function(k,v){
				grid_selector.jqGrid("addRowData",(k+1),v,"last");
			});
		},
		saveCpkz : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			var grid_selector = el.find("#modalGrid");
			var ids = grid_selector.jqGrid("getGridParam","selarrrow");
			t.getCpkzMaxNum();
			var cpkz = param.cpkzMax+1;
			param.cpkzMax = cpkz;
			
			if(ids.length<2){
				jf_alert("请选择2个菜品以上")
			}
			//设置标记
			$(ids).each(function(k,v){
//				$(".each_undish[id="+param.ds[parseInt(v)-1].xh+"]").find(".dish_cpkz").html($.i18n.propJsp("拼")+cpkz).removeClass("hide");
				param.ds[parseInt(v)-1].cpkz = cpkz;
			})
			
			$(param.ds).each(function(k,v){
				if(v.cpkz){
					var filter = "select * from json where (cpkz=="+v.cpkz+")";
		    		var dd = jsonsql.query(filter,param.ds);
		    		if(dd.length==1){
//		    			$(".each_undish[data-bh="+v.xh+"]").find(".dish_cpkz").html("").addClass("hide"); 
		    			$(".each_undish[id="+(k+1)+"]").find(".dish_cpkz").html("").addClass("hide"); 
		    			v.cpkz = ""; 
		    		}
				}
				
				if(v.cpkz==cpkz){
					$(".each_undish[id="+(k+1)+"]").find(".dish_cpkz").html($.i18n.propJsp("拼")+cpkz).removeClass("hide");
				}
				
			})
			t.initGridDs();
		},
		saveCancelCpkz : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			var grid_selector = el.find("#modalGrid");
			var ids = grid_selector.jqGrid("getGridParam","selarrrow");
			var cpkz = "";
			
			//设置标记
			$(ids).each(function(k,v){
//				$(".each_undish[id="+param.ds[parseInt(v)-1].xh+"]").find(".dish_cpkz").html($.i18n.propJsp("拼")+cpkz).removeClass("hide");
				param.ds[parseInt(v)-1].cpkz = cpkz;
			})
			
			
			$(param.ds).each(function(k,v){
				if(v.cpkz){
					var filter = "select * from json where (cpkz=="+v.cpkz+")";
		    		var dd = jsonsql.query(filter,param.ds);
		    		if(dd.length==1){
//		    			$(".each_undish[data-bh="+v.xh+"]").find(".dish_cpkz").html("").addClass("hide"); 
		    			$(".each_undish[id="+(k+1)+"]").find(".dish_cpkz").html("").addClass("hide"); 
		    			v.cpkz = "";
		    		}
				}
				
				if(v.cpkz==cpkz){
					$(".each_undish[id="+(k+1)+"]").find(".dish_cpkz").html("").addClass("hide"); 
				}
				
			})
			t.initGridDs();
		},
		saveAllCpkz : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			param.cpkzMax = 1;
			
			//设置标记
			$(param.ds).each(function(k,v){
				$(".each_undish[data-bh="+v.bh+"]").find(".dish_cpkz").html($.i18n.propJsp("拼")+param.cpkzMax).removeClass("hide");
				v.cpkz = param.cpkzMax;
			})
			t.initGridDs();
		},
		getCpkzMaxNum : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			var cpkzArr = [];
			var cpkzMax = 0;
			$(".dish_cpkz").each(function(k,v){
				var html = $(v).html();
				if(html){
					var num = parseInt($.trim(html.replace($.i18n.propJsp("拼"),"")))
					if(cpkzMax<num){
						cpkzMax = num
					}
				}
			});
			param.cpkzMax = cpkzMax;
		}
	}
	
	function sortByKey(array, key) {
		return array.sort(function(a, b) {
			var x = parseInt(a[key]);
			var y = parseInt(b[key]);
			return ((x < y) ? -1 : ((x > y) ? 1 : 0));
		});
	}
	
})(jQuery);